package com.lanou3g.k12.service.impl;

import com.github.pagehelper.PageHelper;
import com.lanou3g.k12.entity.Event;
import com.lanou3g.k12.entity.EventContent;
import com.lanou3g.k12.mapper.EventContentMapper;
import com.lanou3g.k12.mapper.EventMapper;
import com.lanou3g.k12.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class EventServiceImpl implements EventService {
    @Autowired
    private EventMapper eventMapper;
    @Autowired
    private EventContentMapper eventContentMapper;


    @Override
    public void addEvent(String title, Long cid, Integer status, String content) {
        Event event = new Event();
        event.setCid(cid);
        event.setContent(content);
        event.setStatus(status);
        event.setTitle(title);
        event.setCreateTime(new Date());
        event.setUpdateTime(new Date());
        eventMapper.addEvent(event);
        EventContent eventContent = new EventContent();
        eventContent.setId(event.getId());
        eventContent.setContent(content);
        eventContentMapper.addEventContent(eventContent);
    }

    @Override
    public List<Event> findAllEvent(Integer page,Integer size) {
        PageHelper.startPage(page,size);
        List<Event> eventList = eventMapper.findAllEvent();
        return eventList;
    }


    @Override
    public Event findById(Long id) {
        Event event = eventMapper.findById(id);
        return event;

    }

    @Override
    public void deleteEventById(Long id) {
        eventContentMapper.deleteEventContentById(id);
        eventMapper.deleteEventById(id);
    }

    @Override
    public void deleteEventsByIds(Long[] ids) {
        eventContentMapper.deleteEventContentsByIds(ids);
        eventMapper.deleteEventsByIds(ids);
    }

    @Override
    public List<Event> findEventByKeyword(Integer page, Integer size, String keyword) {
        PageHelper.startPage(page,size);
        List<Event> eventList= eventMapper.findAllEventByKeyword("%" + keyword + "%");
        return eventList;
    }

    @Override
    public List<EventContent> findContentById(Long id) {
        List <EventContent> eventContents = eventContentMapper.findContentById(id);
        return eventContents;
    }

    @Override
    public void updateStatus(Long id) {
        eventMapper.updateStatus(id);
    }

    @Override
    public void updateEventStatus(Long cid) {
        eventMapper.updateEventStatus(cid);
    }

    @Override
    public void deleteEventByCid(Long cid) {
        eventMapper.deleteEventByCid(cid);
    }

    @Override
    public List<Event> findEventByCid(Long cid) {
        List<Event> eventList = eventMapper.findEventByCid(cid);
        return eventList;
    }

    @Override
    public Event findEventContentById(Long id) {
        Event event = eventMapper.findEventContentById(id);
        return event;
    }

    @Override
    public void updateEventContentById(EventContent eventContent) {
       eventContentMapper.updateEventContentById(eventContent);
    }
}
